<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_member_ippi extends CI_Controller {
    var $folder_view = "/member_ippi/";
    function __construct() {
        parent::__construct();
        $this->load->model('model_member', 'm_member', TRUE);
        $this->load->model('model_state', 'm_state', TRUE);
    }
    
    function index() {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            $data = array(
                'web_title' =>  'Member IPPI',
                'main_view' =>  'mimin'.$this->folder_view.'index_new',
                'list_data' =>  $this->m_member->get_all_ippi_member()->result(),
            );
            $this->load->view('mimin/template/template_table',$data);
        }
    }
    
    /*
     * add form member ippi
     * 141123 dinov
     */
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        else{
            $this->form_validation->set_rules('email','email','required');
            $this->form_validation->set_rules('name','Nama','required');
            $this->form_validation->set_rules('password','Password','required');
            if($this->form_validation->run() == FALSE){
                // set common properties
                $data = array(
                    'web_title'     =>  'Data Anggota IPPI Baru',
                    'main_view'     =>  "mimin".$this->folder_view."add",
                    "list_state"    =>  $this->m_state->get_all(),
                );
                $this->parser->parse("mimin/template/template_editor",$data);
            }
            else{
                extract($_POST);
                $count = $this->m_member->count_by_email($email);
                if($count > 1){
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "<b>".$email."</b> already registered",
                    );
                    echo json_encode($output);exit();
                }
                else{
                    //SET TIME ZONE
                    date_default_timezone_set("Asia/Jakarta");
                    $curdate = date("Y-m-d h:i:s");
                    
                    $isi_new = array(
                        'email' =>  $email,
                        'name'  =>  $name,
                        'password'  =>  md5("12345"),
                        'datecreated'  =>  $curdate,
                    );
                }
            }
        }
    }
    function add_act(){
        $this->form_validation->set_rules('email','email','required');
        $this->form_validation->set_rules('name','Nama','required');
        $this->form_validation->set_rules('alamat','alamat','required');
        $this->form_validation->set_rules('province','province','required');
        $this->form_validation->set_rules('city','city','required');
        $this->form_validation->set_rules('tanggal_lahir_member','tanggal_lahir_member','required');
        $this->form_validation->set_rules('tempat_lahir','tempat_lahir','required');
        $this->form_validation->set_rules('pekerjaan','pekerjaan','required');
        $this->form_validation->set_rules('income','income','required');
        $this->form_validation->set_rules('pendidikan','pendidikan','required');
        $this->form_validation->set_rules('hp','hp','required');
        $this->form_validation->set_rules('status_menikah','status_menikah','required');
        $this->form_validation->set_rules('status_vct','status_vct','required');
        $this->form_validation->set_rules('status_arv','status_arv','required');
        $this->form_validation->set_rules('jumlah_anak_laki','jumlah_anak_laki','required');
        $this->form_validation->set_rules('jumlah_anak_pr','jumlah_anak_pr','required');
        $this->form_validation->set_rules('motivasi','motivasi','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "All fields must be filled",
            );
            echo json_encode($output);exit();
        }
            
        //cek data
        $condition_member = array(
            "email" =>  $this->input->post("email")
        );
        $list_data = $this->m_member->get_by_codition($condition_member);
        if($list_data->num_rows() > 0){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "Email address already used",
            );
            exit(json_encode($output));
        }
        
        //SET TIME ZONE
        date_default_timezone_set("Asia/Jakarta");
        $curdate = date("Y-m-d H:i:s");
        //TRANSACTION BEGIN
        $this->db->trans_begin();
        //save to tbl_member
        $isi_new_member = array(
            'email'         =>  $this->input->post("email"),
            'name'          =>  $this->input->post("name"),
            'password'      =>  md5("12345"),
            'datecreated'   =>  $curdate,
        );
        $status_save = $this->m_member->save_data($isi_new_member);
        if(!$status_save){
            $this->db->trans_rollback();
            $output = array(
                'status'    =>  0,
                'msg'       =>  "Data is not saved",
            );
            exit(json_encode($output));
        }
        //save to tbl_member_ippi
        $isi_new_ippi = array(
            "email"                     =>  $this->input->post("email"),
            "provinsi"                  =>  $this->input->post("province"),
            "alamat"                    =>  $this->input->post("alamat"),
            "kota"                      =>  $this->input->post("city"),
            "tanggal_lahir"             =>  $this->input->post("tanggal_lahir_member"),
            "tempat_lahir"              =>  $this->input->post("tempat_lahir"),
            "pekerjaan"                 =>  $this->input->post("pekerjaan"),
            "penghasilan_rata_perbulan" =>  $this->input->post("income"),
            "pendidikan"                =>  $this->input->post("pendidikan"),
            "telp_rumah"                =>  $this->input->post("telp_rumah"),
            "telp_kantor"               =>  $this->input->post("telp_kantor"),
            "hp"                        =>  $this->input->post("hp"),
            "fax"                       =>  $this->input->post("fax"),
            "status_menikah"            =>  $this->input->post("status_menikah"),
            "status_vct"                =>  $this->input->post("status_vct"),
            "vct_diket"                 =>  $this->input->post("tahun_vct"),
            "status_arv"                =>  $this->input->post("status_arv"),
            "jenis_arv"                 =>  $this->input->post("namejenis_arv"),
            "jml_anak_laki"             =>  $this->input->post("jumlah_anak_laki"),
            "jml_anak_pr"               =>  $this->input->post("jumlah_anak_pr"),
            "tgl_lhr_anak1"             =>  $this->input->post("tanggal_lahir_anak1"),
            "tgl_lhr_anak2"             =>  $this->input->post("tanggal_lahir_anak2"),
            "tgl_lhr_anak3"             =>  $this->input->post("tanggal_lahir_anak3"),
            "tgl_lhr_anak4"             =>  $this->input->post("tanggal_lahir_anak4"),
            "nama_anak1"                =>  $this->input->post("nama_anak1"),
            "nama_anak2"                =>  $this->input->post("nama_anak2"),
            "nama_anak3"                =>  $this->input->post("nama_anak3"),
            "nama_anak4"               =>  $this->input->post("nama_anak4"),
            "pendidikan_anak1"          =>  $this->input->post("pendidikan_anak"),
            "status_vct_anak"           =>  $this->input->post("status_vct_anak"),
            "status_arv_anak"           =>  $this->input->post("status_arv_anak"),
            "motivasi"                  =>  $this->input->post("motivasi"),
        );
        $status_save_ippi_member = $this->m_member->save_data_ippi_member($isi_new_ippi);
        if(!$status_save_ippi_member){
            $this->db->trans_rollback();
            $output = array(
                'status'    =>  0,
                'msg'       =>  "Data is not saved",
            );
            exit(json_encode($output));
        }
        
        //SUCCESS
        $this->db->trans_commit();
            $output = array(
                'status'    =>  1,
                'msg'       =>  "new Data is saved",
            );
            exit(json_encode($output));
    }
    
    
    
    
    function delete(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id member','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Member is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            $count = $this->m_member->select_by_id($id,"count");
            if($count > 0){
                $list_data = $this->m_member->select_by_id($id,"result");
                $status_deleted = "";
                foreach($list_data as $r):
                    $status_deleted = $r->deleted;
                endforeach;
                
                $this->db->trans_begin();
                
                if($status_deleted != 1){
                    $update_data = array(
                        'deleted'    =>  1
                    );
                    $status_update = $this->m_member->update_data($id,$update_data);
                    if($status_update){
                        if ($this->db->trans_status() === FALSE)
                        {
                            $this->db->trans_rollback();
                            $output = array(
                                'status'    =>  0,
                                'msg'       =>  "Data deletion is failed, ERROR : ".  mysql_error()
                            );
                            echo json_encode($output);exit();
                        }
                        else
                        {
                            $this->db->trans_commit();
                            $output = array(
                                'status'    =>  1,
                                'msg'       =>  "Succesfully Change data status to deleted"
                            );
                            echo json_encode($output);exit();
                        }
                    }
                    else{
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Delete member data unsuccessful, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    //delete permanently

                    $status_delete = $this->m_member->delete($id);
                    if($status_delete){
                        if ($this->db->trans_status() === FALSE)
                        {
                            $this->db->trans_rollback();
                            $output = array(
                                'status'    =>  0,
                                'msg'       =>  "Delete member data permanently unsuccessful"
                            );
                            echo json_encode($output);exit();
                        }
                        else
                        {
                            $this->db->trans_commit();
                            $output = array(
                                'status'    =>  1,
                                'msg'       =>  "success Delete member data permanently"
                            );
                            echo json_encode($output);exit();
                        }
                    }
                    else{
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Delete member data permanently unsuccessful, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    function undelete(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id member','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Member is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            $count = $this->m_member->select_by_id($id,"count");
            if($count > 0){
                $this->db->trans_begin();
                $update_data = array(
                    'deleted'    =>  0
                );
                $status_update = $this->m_member->update_data($id,$update_data);
                if($status_update){
                    if ($this->db->trans_status() === FALSE)
                    {
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Data restoration is failed, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                    else
                    {
                        $this->db->trans_commit();
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Succesfully restore data to deleted"
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "restoration data unsuccessful, ERROR : ".  mysql_error()
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    function block(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id member','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Member is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            $count  = $this->m_member->select_by_id($id,"count");
            if($count > 0){
                $data   = $this->m_member->select_by_id($id);
                $email="";
                foreach ($data as $r) {
                    $email = $r->email;
                }
                $this->db->trans_begin();
                $update_data = array(
                    'status'    =>  0
                );
                $status_update = $this->m_member->update_data($id,$update_data);
                if($status_update){
                    if ($this->db->trans_status() === FALSE)
                    {
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Blocking member is failed, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                    else
                    {
                        $this->db->trans_commit();
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Succesfully block member ".$email
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Blocking data unsuccessful, ERROR : ".  mysql_error()
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    function unblock(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id member','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Member is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            $count  = $this->m_member->select_by_id($id,"count");
            if($count > 0){
                $data   = $this->m_member->select_by_id($id);
                $email="";
                foreach ($data as $r) {
                    $email = $r->email;
                }
                $this->db->trans_begin();
                $update_data = array(
                    'status'    =>  1
                );
                $status_update = $this->m_member->update_data($id,$update_data);
                if($status_update){
                    if ($this->db->trans_status() === FALSE)
                    {
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Unlocking member is failed, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                    else
                    {
                        $this->db->trans_commit();
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Succesfully unblock member ".$email
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Unblocking data unsuccessful, ERROR : ".  mysql_error()
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    
    
    function detail($id=""){
        //SET TIME ZONE
        date_default_timezone_set("Asia/Jakarta");
        $curdate = date("Y-m-d h:i:s");
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        elseif($id==""){
            redirect("admin_member","refresh");
        }
        else{
            $count = $this->m_member->select_by_id($id,"count");
            if($count > 0){
                $data = array(
                    "main_view"     =>  "mimin/member/detail",
                    "web_title"     =>  "Detail Member Data",
                    "list_data"     =>  $this->m_member->select_by_id($id)
                );
                $this->load->view("mimin/template/template_editor",$data);
            }
            else{
                redirect("admin_news","refresh");
            }
        }
    }
    function detail_act(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    => 0,
                'msg'       => "You need to re-login the system",
            );
            echo json_encode($output);exit();
        }
        elseif($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    => 0,
                'msg'       => "You have no access level",
            );
            echo json_encode($output);exit();
        }
        else{
            $this->form_validation->set_rules('id','id','required');
            $this->form_validation->set_rules('name','Nama','required');
            $this->form_validation->set_rules('status','Nama','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "All fields must be filled",
                );
                echo json_encode($output);exit();
            }
            else{
                extract($_POST);
                $count = $this->m_member->select_by_id($id,"count");
                if($count > 0){
                    //SET TIME ZONE
                    date_default_timezone_set("Asia/Jakarta");
                    $curdate = date("Y-m-d h:i:s");
                    
                    $isi_update = array(
                        'name'          =>  $name,
                        'status'        =>  $status,
                        'datemodified'  =>  $curdate,
                    );
                    $status_update = $this->m_member->update_data($id,$isi_update);
                    if($status_update){
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Data has been updated",
                        );
                        echo json_encode($output);exit();
                    }
                    else{
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Failed update data ",
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  " The data might be deleted, please refresh the page",
                    );
                    echo json_encode($output);exit();
                }
            }
        }
    }
}

/* End of file admin_member_ippi.php */
/* Location: ./application/controllers/admin_member_ippi.php */